package com.xnyzc.lhy.mis.service.impl.cms.vehicle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.penum.cms.veh.EVehFuelType;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.area.OaSysArea;
import com.xnyzc.lhy.mis.entity.result.cms.veh.VehicleManageInfo;
import com.xnyzc.lhy.mis.entity.dictionaries.OaSysDict;
import com.xnyzc.lhy.mis.entity.user.OaDUser;
import com.xnyzc.lhy.mis.entity.vehicle.OaCVehicleInsurance;
import com.xnyzc.lhy.mis.entity.vehicle.OaSysVehicle;
import com.xnyzc.lhy.mis.entity.vehicle.OaSysVehicleMdict;
import com.xnyzc.lhy.mis.mapper.area.OaSysAreaMapper;
import com.xnyzc.lhy.mis.mapper.dictionaries.OaSysDictMapper;
import com.xnyzc.lhy.mis.mapper.user.OaDUserMapper;
import com.xnyzc.lhy.mis.mapper.vehicle.OaCVehicleInsuranceMapper;
import com.xnyzc.lhy.mis.mapper.vehicle.OaSysVehicleMapper;
import com.xnyzc.lhy.mis.mapper.vehicle.OaSysVehicleMdictMapper;
import com.xnyzc.lhy.mis.mapper.vehicle.OaVehicleSeriesMapper;
import com.xnyzc.lhy.mis.service.cms.util.IOaSysGetOfficeIdListService;
import com.xnyzc.lhy.mis.service.cms.vehicle.VehicleManageService;
import com.xnyzc.lhy.mis.service.impl.cms.util.OaSysGetOfficeIdListServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

/**
 * @author zhaolijie
 * @date 2019-08-24
 */
@Service
public class VehicleManageServiceImpl implements VehicleManageService {

    private static final String UPDATE_OPERATE = "update";
    private static final String SAVE_OPERATE = "save";
    private static final String REMOVE_OPERATE = "remove";

    @Autowired
    private OaSysVehicleMapper oaSysVehicleMapper;

    @Autowired
    private OaSysDictMapper oaSysDictMapper;

    @Autowired
    private OaSysVehicleMdictMapper oaSysVehicleMdictMapper;

    @Autowired
    private OaVehicleSeriesMapper oaVehicleSeriesMapper;

    @Autowired
    private OaSysAreaMapper oaSysAreaMapper;

    @Autowired
    private OaDUserMapper oaDUserMapper;

    @Autowired
    private OaCVehicleInsuranceMapper vehicleInsuranceMapper;

    @Autowired
    private IOaSysGetOfficeIdListService oaSysGetOfficeIdListService;


    /**
     * 根据车牌号查询车辆保险信息
     * @param veho
     * @return
     */
    @Override
    public Rv getVehicleInsuranceInfoByVeho(String veho) {
        long userId = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(veho) ||
        CheckUtil.objIsEmpty(userId) ||
        CheckUtil.objIsEmpty(TokenUtil.getTag())) {
            return Rv.error(EErrorCode.missingAuthArgs);
        }
        QueryWrapper<OaCVehicleInsurance> wrapper = new QueryWrapper<>();
        wrapper.eq(OaCVehicleInsurance.VEH_NO,veho);
        OaCVehicleInsurance insuranceInfo = vehicleInsuranceMapper.selectOne(wrapper);
        return Rv.wrap(insuranceInfo);
    }

    /**
     * 车辆保险信息crud操作
     *
     * @param vehicleInsurance
     * @return
     */
    @Override
    public Rv vehicleInsuranceOperate(OaCVehicleInsurance vehicleInsurance) {
        long userId = TokenUtil.getTokenUserId();
        if (CheckUtil.objIsEmpty(userId) || CheckUtil.objIsEmpty(TokenUtil.getTag())) {
            return Rv.error(EErrorCode.missingAuthArgs);
        }
        if(CheckUtil.objIsEmpty(vehicleInsurance.getVehNo()) || vehicleInsurance.getVehNo()==""){
            return Rv.error(EErrorCode.missingArg);
        }
        //校验库中有无该车牌号的车
        QueryWrapper<OaSysVehicle> oaSysVehicleQueryWrapper = new QueryWrapper<>();
        oaSysVehicleQueryWrapper.eq(OaSysVehicle.VEH_NO, vehicleInsurance.getVehNo());
        oaSysVehicleQueryWrapper.eq(OaSysVehicle.DELETED, 0);
        OaSysVehicle oaSysVehicle = oaSysVehicleMapper.selectOne(oaSysVehicleQueryWrapper);
        if (CheckUtil.objIsEmpty(oaSysVehicle)) {
            return Rv.error(EErrorCode.noVehicleError);
        }
        int isSuccess = 0;
        if (CheckUtil.objIsNotEmpty(vehicleInsurance.getFlag()) && vehicleInsurance.getFlag() == 3) {
            // 删除操作
            isSuccess = operateInsuranceInfo(vehicleInsurance, userId, REMOVE_OPERATE);
        }
        //参数中id号不为空则更新操作
        if (!CheckUtil.objIsEmpty(vehicleInsurance.getInsuranceId())) {
            //更新操作
            isSuccess = operateInsuranceInfo(vehicleInsurance, userId, UPDATE_OPERATE);
        } else {
            //二次验证 防止id号未传过来 根据车牌号在保险表中查找
            QueryWrapper<OaCVehicleInsurance> insuranceQueryWrapper = new QueryWrapper<>();
            insuranceQueryWrapper.eq(OaCVehicleInsurance.VEH_NO, vehicleInsurance.getVehNo());
            insuranceQueryWrapper.eq(OaCVehicleInsurance.DELETED, 0);
            OaCVehicleInsurance oacVehicleInsurance = vehicleInsuranceMapper.selectOne(insuranceQueryWrapper);
            if (!CheckUtil.objIsEmpty(oacVehicleInsurance)) {
                vehicleInsurance.setInsuranceId(oacVehicleInsurance.getInsuranceId());
                isSuccess = operateInsuranceInfo(vehicleInsurance, userId, UPDATE_OPERATE);
            } else {
                isSuccess = operateInsuranceInfo(vehicleInsurance, userId, SAVE_OPERATE);
            }
        }
        return Rv.wrap(isSuccess);
    }

    private int operateInsuranceInfo(OaCVehicleInsurance vehicleInsurance, Long userId, String flag) {
        // 0:success 1:fail
        int isSuccess = 0;
        if (flag.equals(UPDATE_OPERATE)) {
            vehicleInsurance.setFlag(2);
            vehicleInsurance.setUpdateTime(new Date());
            vehicleInsurance.setUpdateUser(userId);
            isSuccess = vehicleInsuranceMapper.updateById(vehicleInsurance);
        } else if (flag.equals(SAVE_OPERATE)) {
            vehicleInsurance.setInsuranceId(Long.valueOf(IDUtil.nextId()));
            vehicleInsurance.setCreateUser(userId);
            vehicleInsurance.setCreateTime(new Date());
            vehicleInsurance.setUpdateTime(new Date());
            vehicleInsurance.setUpdateUser(userId);
            isSuccess = vehicleInsuranceMapper.insert(vehicleInsurance);
        } else {
            vehicleInsurance.setDeleted(1);
            vehicleInsurance.setUpdateTime(new Date());
            vehicleInsurance.setUpdateUser(userId);
            isSuccess = vehicleInsuranceMapper.updateById(vehicleInsurance);
        }
        return isSuccess;
    }


    public static JSONArray getVehicleSplicingData(JSONArray classAJSONArray, JSONArray classBJSONArray, JSONArray classCJSONArray,
                                                   String classACodeKey, String classBCodekey, String classCodeKey,
                                                   String nameClassAKey, String nameClassBkey, String nameClassCKey,
                                                   String tableCodeName, String primaryKeyName
    ) {
        for (int i = 0; i < classCJSONArray.size(); i++) {
            JSONObject oaSysVehicleMdictsCJSONArrayJSONObject = classCJSONArray.getJSONObject(i);
            oaSysVehicleMdictsCJSONArrayJSONObject.put(classCodeKey, oaSysVehicleMdictsCJSONArrayJSONObject.getString(tableCodeName));
            oaSysVehicleMdictsCJSONArrayJSONObject.put(nameClassCKey, oaSysVehicleMdictsCJSONArrayJSONObject.getString("name"));
            for (int j = 0; j < classBJSONArray.size(); j++) {
                JSONObject oaSysVehicleMdictsBJSONArrayJSONObject = classBJSONArray.getJSONObject(j);
                if (oaSysVehicleMdictsCJSONArrayJSONObject.getString("parentId").equals(oaSysVehicleMdictsBJSONArrayJSONObject.getString(primaryKeyName))) {
                    oaSysVehicleMdictsCJSONArrayJSONObject.put(classBCodekey, oaSysVehicleMdictsBJSONArrayJSONObject.getString(tableCodeName));
                    oaSysVehicleMdictsCJSONArrayJSONObject.put(nameClassBkey, oaSysVehicleMdictsBJSONArrayJSONObject.getString("name"));
                    for (int k = 0; k < classAJSONArray.size(); k++) {
                        JSONObject oaSysVehicleMdictsAJSONArrayJSONObject = classAJSONArray.getJSONObject(k);
                        if (oaSysVehicleMdictsBJSONArrayJSONObject.getString("parentId").equals(oaSysVehicleMdictsAJSONArrayJSONObject.getString(primaryKeyName))) {
                            oaSysVehicleMdictsCJSONArrayJSONObject.put(classACodeKey, oaSysVehicleMdictsAJSONArrayJSONObject.getString(tableCodeName));
                            oaSysVehicleMdictsCJSONArrayJSONObject.put(nameClassAKey, oaSysVehicleMdictsAJSONArrayJSONObject.getString("name"));
                        }
                    }
                }
            }
        }
        return classCJSONArray;
    }


    private List<VehicleManageInfo> getInfoList(List<VehicleManageInfo> vehicleList) {
        // 车辆code码List
        List<String> vehicleCodeList = new ArrayList<>();
        // 城市code码List
        List<String> cityCodeList = new ArrayList<>();
        for (int i = 0; i < vehicleList.size(); i++) {
            vehicleCodeList.add(vehicleList.get(i).getCode());
            cityCodeList.add(String.valueOf(vehicleList.get(i).getCityCode()));
        }
        // 车辆字典表里查询到的3级车辆信息
        List<OaSysVehicleMdict> oaSysVehicleMdictsC = this.findVehicleMdict(vehicleCodeList, OaSysVehicleMdict.CODE);
        if (CheckUtil.objIsEmpty(oaSysVehicleMdictsC)) {
            return vehicleList;
        }

        // 装载二级菜单的List
        List<String> parentIdVehicleB = new ArrayList<>();
        for (int i = 0; i < oaSysVehicleMdictsC.size(); i++) {
            parentIdVehicleB.add(oaSysVehicleMdictsC.get(i).getParentId());
        }
        //车辆字典表里查询到的2级车辆信息
        List<OaSysVehicleMdict> oaSysVehicleMdictsB = this.findVehicleMdict(parentIdVehicleB, OaSysVehicleMdict.MDICT_ID);

        // 装载一级菜单的List
        List<String> parentIdVehicleA = new ArrayList<>();
        for (int i = 0; i < oaSysVehicleMdictsB.size(); i++) {
            parentIdVehicleA.add(oaSysVehicleMdictsB.get(i).getParentId());
        }
        // 车辆字典表里查询到的1级车辆信息
        List<OaSysVehicleMdict> oaSysVehicleMdictsA = this.findVehicleMdict(parentIdVehicleA, OaSysVehicleMdict.MDICT_ID);

        JSONArray oaSysVehicleMdictsCJSONArray = JSONArray.parseArray(JSON.toJSONString(oaSysVehicleMdictsC));
        JSONArray oaSysVehicleMdictsBJSONArray = JSONArray.parseArray(JSON.toJSONString(oaSysVehicleMdictsB));
        JSONArray oaSysVehicleMdictsAJSONArray = JSONArray.parseArray(JSON.toJSONString(oaSysVehicleMdictsA));
        JSONArray splicingData = VehicleManageServiceImpl.getVehicleSplicingData(
                oaSysVehicleMdictsAJSONArray, oaSysVehicleMdictsBJSONArray, oaSysVehicleMdictsCJSONArray,
                "vehicleCode1", "vehicleCode2", "vehicleCode3",
                "brand", "model", "vehicleTypeName",
                "code", "mdictId"
        );
        for (int i = 0; i < vehicleList.size(); i++) {
            for (int j = 0; j < splicingData.size(); j++) {
                if (CheckUtil.strIsNotEmpty(vehicleList.get(i).getCode()) && vehicleList.get(i).getCode().equals(splicingData.getJSONObject(j).getString("code"))) {
                    vehicleList.get(i).setBrand(splicingData.getJSONObject(j).getString("brand"));
                    vehicleList.get(i).setModel(splicingData.getJSONObject(j).getString("model"));
                    vehicleList.get(i).setVehicleTypeName(splicingData.getJSONObject(j).getString("vehicleTypeName"));
                    vehicleList.get(i).setVehicleCodeClassA(splicingData.getJSONObject(j).getString("vehicleCode1"));
                    vehicleList.get(i).setVehicleCodeClassB(splicingData.getJSONObject(j).getString("vehicleCode2"));
                    vehicleList.get(i).setVehicleCodeClassC(splicingData.getJSONObject(j).getString("vehicleCode3"));
                }
            }
        }

        // 城市查询市级
        List<OaSysArea> cityMdictB = this.findCityMdict(cityCodeList, OaSysArea.AD_CODE);
        List<String> parentIdCityB = new ArrayList<>();
        for (int i = 0; i < cityMdictB.size(); i++) {
            parentIdCityB.add(cityMdictB.get(i).getParentId());
        }
        List<OaSysArea> cityMdictA = this.findCityMdict(parentIdCityB, OaSysArea.AREA_ID);

        JSONArray cityMdictBJsonArray = JSONArray.parseArray(JSON.toJSONString(cityMdictB));
        JSONArray cityMdictAJsonArray = JSONArray.parseArray(JSON.toJSONString(cityMdictA));
        for (int i = 0; i < cityMdictBJsonArray.size(); i++) {
            JSONObject cityMdictBJsonArrayJSONObject = cityMdictBJsonArray.getJSONObject(i);
            cityMdictBJsonArrayJSONObject.put("cityCodeClassB", cityMdictBJsonArrayJSONObject.getString("adCode"));
            cityMdictBJsonArrayJSONObject.put("city", cityMdictBJsonArrayJSONObject.getString("name"));
            for (int j = 0; j < cityMdictAJsonArray.size(); j++) {
                JSONObject cityMdictAJsonArrayJSONObject = cityMdictAJsonArray.getJSONObject(j);
                if (cityMdictBJsonArrayJSONObject.getString("parentId").equals(cityMdictAJsonArrayJSONObject.getString("areaId"))) {
                    cityMdictBJsonArrayJSONObject.put("cityCodeClassA", cityMdictAJsonArrayJSONObject.getString("adCode"));
                    cityMdictBJsonArrayJSONObject.put("province", cityMdictAJsonArrayJSONObject.getString("name"));
                }
            }
        }

        for (int i = 0; i < vehicleList.size(); i++) {
            for (int j = 0; j < cityMdictBJsonArray.size(); j++) {
                if (CheckUtil.objIsNotEmpty(vehicleList.get(i).getCityCode()) && String.valueOf(vehicleList.get(i).getCityCode()).equals(cityMdictBJsonArray.getJSONObject(j).getString("adCode"))) {
                    vehicleList.get(i).setProvince(cityMdictBJsonArray.getJSONObject(j).getString("province"));
                    vehicleList.get(i).setCity(cityMdictBJsonArray.getJSONObject(j).getString("city"));
                    vehicleList.get(i).setCityCodeClassA(cityMdictBJsonArray.getJSONObject(j).getString("cityCodeClassA"));
                    vehicleList.get(i).setCityCodeClassB(cityMdictBJsonArray.getJSONObject(j).getString("cityCodeClassB"));
                }
            }
        }
        return vehicleList;
    }


    /**
     * 分页获取车辆信息
     *
     * @param vehicleManageInfo
     * @return
     */
    @Override
    public Rv getVehicleList(VehicleManageInfo vehicleManageInfo) {
        Long userId = TokenUtil.getTokenUserId();
        List<String> types = TokenUtil.getRoleDataScope();
        // 设置分页信息
        IPage iPage = new Page();
        iPage.setCurrent(vehicleManageInfo.getParamsCurrent());
        iPage.setSize(vehicleManageInfo.getParamSize());
        List<Long> dusers;

        Long officeId = vehicleManageInfo.getOfficeId();
        if (officeId == null) {
            dusers = oaSysGetOfficeIdListService.selectDUserIdList(userId, types);
        } else {
            dusers = oaSysGetOfficeIdListService.selectDUserIdList(officeId);
        }
        if (dusers.size() == 0) {
            return Rv.wrap();
        }

        // 查询车辆信息
        List<VehicleManageInfo> vehicleList = oaSysVehicleMapper.getVehicleList(iPage, vehicleManageInfo.getParamsVehicleNo(), vehicleManageInfo.getParamsCityCode(), vehicleManageInfo.getParamsVehFrameNumber(), vehicleManageInfo.getParamsCode(), vehicleManageInfo.getParamsDriveName(), dusers);
        if (CheckUtil.objIsEmpty(vehicleList)) {
            return Rv.wrap(EErrorCode.success, iPage);
        }
        List<VehicleManageInfo> infoList = this.getInfoList(vehicleList);
        iPage.setRecords(infoList);
        return Rv.wrap(EErrorCode.success, iPage);
    }


    /**
     * 查询车辆详情
     *
     * @param vehicleId ：车辆Id
     * @return
     */
    @Override
    public Rv getVehicleDetails(Long vehicleId) {
        // 查询车辆详情信息
        List<VehicleManageInfo> vehicleDetails = oaSysVehicleMapper.getVehicleDetails(vehicleId);
        if (vehicleDetails.isEmpty() && CollectionUtils.isEmpty(vehicleDetails)) {
            return Rv.wrap(EErrorCode.noData, EErrorCode.noData.getDesc());
        }
        List<VehicleManageInfo> list = this.getInfoList(vehicleDetails);
        QueryWrapper<OaDUser> queryWrapper = new QueryWrapper();
        queryWrapper.select(OaDUser.DRIVER_NAME);
        queryWrapper.eq(OaDUser.DRIVER_ID, list.get(0).getVehicleOwnerId());
        OaDUser oaDUser = oaDUserMapper.selectOne(queryWrapper);
        if (null != oaDUser && null != oaDUser.getDriverName()) {
            list.get(0).setVehicleOwnerName(oaDUser.getDriverName());
        }
        QueryWrapper<OaSysDict> queryWrapperGetRequireLevel = new QueryWrapper<>();
        queryWrapperGetRequireLevel.eq(OaSysDict.TYPE, "require_level");
        List<OaSysDict> oaSysDicts = oaSysDictMapper.selectList(queryWrapperGetRequireLevel);
        for (int i = 0; i < oaSysDicts.size(); i++) {
            if (list.get(0).getVehicleType() == null) {
                list.get(0).setRequireLevelName("未分类");
            } else {
                if (list.get(0).getVehicleType().equals(oaSysDicts.get(i).getValue())) {
                    list.get(0).setRequireLevelName(oaSysDicts.get(i).getLabel());
                }
            }

        }
        return Rv.wrap(EErrorCode.success, list);
    }

    /**
     * 获取司机信息
     *
     * @return
     */
    @Override
    public Rv getDriverInfo(Integer current, Integer pageSize, String driverName, String driverPhone) {
        IPage iPage = new Page();
        iPage.setSize(pageSize);
        iPage.setCurrent(current);
        QueryWrapper<OaDUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(OaDUser.DRIVER_NAME, driverName);
        queryWrapper.like(OaDUser.DRIVER_PHONE, driverPhone);
        queryWrapper.select(OaDUser.DRIVER_ID, OaDUser.DRIVER_NAME, OaDUser.DRIVER_PHONE);
        IPage selectPage = oaDUserMapper.selectPage(iPage, queryWrapper);
        return Rv.wrap(EErrorCode.success, selectPage);
    }

    /**
     * 添加车辆信息
     *
     * @param oaSysVehicle：车辆信息表实体类
     * @return
     */
    @Override
    public Rv addVehicle(OaSysVehicle oaSysVehicle) {
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)) {
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        QueryWrapper<OaSysVehicle> queryWrapperVehNo = new QueryWrapper<>();
        queryWrapperVehNo.eq(OaSysVehicle.VEH_NO, oaSysVehicle.getVehNo());
        List<OaSysVehicle> oaSysVehicles = oaSysVehicleMapper.selectList(queryWrapperVehNo);
        if (oaSysVehicles.size() == 0) {
            QueryWrapper<OaSysVehicleMdict> queryWrapperMdict = new QueryWrapper<>();
            queryWrapperMdict.eq(OaSysVehicleMdict.MDICT_ID, oaSysVehicle.getCode());
            OaSysVehicleMdict oaSysVehicleMdict = oaSysVehicleMdictMapper.selectOne(queryWrapperMdict);
            oaSysVehicle.setCode(oaSysVehicleMdict.getCode());
            oaSysVehicle.setVehStatus(0);
            oaSysVehicle.setVehicleId(IDUtil.nextId());
            oaSysVehicle.setTag(tag);
            oaSysVehicle.setCreateTime(new Date());
            oaSysVehicle.setCreateUser(TokenUtil.getTokenUserId());
            oaSysVehicle.setUpdateTime(new Date());
            oaSysVehicleMapper.insert(oaSysVehicle);
            return Rv.wrap(EErrorCode.success);
        } else {
            return Rv.wrap(EErrorCode.beUsed);
        }
    }

    /**
     * 新增车辆信息
     *
     * @param oaSysVehicle：车辆信息表实体类
     * @return
     */
    @Override
    public Rv addNewVehicle(OaSysVehicle oaSysVehicle) {
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)) {
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        QueryWrapper<OaSysVehicle> queryWrapperVehNo = new QueryWrapper<>();
        queryWrapperVehNo.eq(OaSysVehicle.VEH_NO, oaSysVehicle.getVehNo());
        List<OaSysVehicle> oaSysVehicles = oaSysVehicleMapper.selectList(queryWrapperVehNo);
        if (oaSysVehicles.size() == 0) {
            oaSysVehicle.setTag(tag);
            oaSysVehicleMapper.insert(oaSysVehicle);
            return Rv.wrap(EErrorCode.success);
        } else {
            return Rv.wrap(EErrorCode.beUsed);
        }
    }
    /**
     * 查询车辆颜色
     *
     * @return
     */
    @Override
    public Rv findVehicleColor() {
        QueryWrapper<OaSysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaSysDict.TYPE, "vegicle_color_type");
        List<OaSysDict> oaSysDicts = oaSysDictMapper.selectList(queryWrapper);
        return Rv.wrap(EErrorCode.success, oaSysDicts);
    }

    /**
     * 修改车辆信息
     *
     * @param vehicleManageInfo
     * @return
     */
    @Override
    public Rv updateVehicle(VehicleManageInfo vehicleManageInfo) {
        QueryWrapper<OaSysVehicle> queryWrapperVehicleNo = new QueryWrapper<>();
        queryWrapperVehicleNo.eq(OaSysVehicle.VEH_NO, vehicleManageInfo.getVehNo());
        queryWrapperVehicleNo.ne(OaSysVehicle.VEHICLE_ID, vehicleManageInfo.getVehicleId());
        List<OaSysVehicle> oaSysVehiclesVehicleNo = oaSysVehicleMapper.selectList(queryWrapperVehicleNo);
        if (oaSysVehiclesVehicleNo.size() > 0) {
            return Rv.wrap(EErrorCode.success, 1);
        }

        if (CheckUtil.strIsNotEmpty(vehicleManageInfo.getVehFrameNumber())) {
            QueryWrapper<OaSysVehicle> queryWrapperVehFrameNumber = new QueryWrapper<>();
            queryWrapperVehFrameNumber.eq(OaSysVehicle.VEH_FRAME_NUMBER, vehicleManageInfo.getVehFrameNumber());
            queryWrapperVehFrameNumber.ne(OaSysVehicle.VEHICLE_ID, vehicleManageInfo.getVehicleId());
            List<OaSysVehicle> oaSysVehiclesVehFrameNumber = oaSysVehicleMapper.selectList(queryWrapperVehFrameNumber);
            if (oaSysVehiclesVehFrameNumber.size() > 0) {
                return Rv.wrap(EErrorCode.success, 2);
            }
        }
        OaSysVehicle oaSysVehicle = new OaSysVehicle();
        oaSysVehicle.setVehicleId(vehicleManageInfo.getVehicleId());
        oaSysVehicle.setVehNo(vehicleManageInfo.getVehNo());
        oaSysVehicle.setVehFrameNumber(vehicleManageInfo.getVehFrameNumber());
        oaSysVehicle.setVehEngineNumber(vehicleManageInfo.getVehEngineNumber());
        //oaSysVehicle.setVehSeatsNumber(vehicleManageInfo.getVehSeatsNumber());
        oaSysVehicle.setVehColor(vehicleManageInfo.getVehColor());
        oaSysVehicle.setDrivingLicenseNumber(vehicleManageInfo.getDrivingLicenseNumber());
        oaSysVehicle.setVehicleOwnerId(vehicleManageInfo.getVehicleOwnerId());
        oaSysVehicle.setTransportCertificateNumber(vehicleManageInfo.getTransportCertificateNumber());
        oaSysVehicle.setVehicleLicensePictureId(vehicleManageInfo.getVehicleLicensePictureId());
        oaSysVehicle.setVehicleLicensePictureBackId(vehicleManageInfo.getVehicleLicensePictureBackId());
        oaSysVehicle.setVehicleLicenseSupplementaryPagePictureId(vehicleManageInfo.getVehicleLicenseSupplementaryPagePictureId());
        oaSysVehicle.setTransportCertificatePictureId(vehicleManageInfo.getTransportCertificatePictureId());
        oaSysVehicle.setVehicleType(vehicleManageInfo.getVehicleType1());
        oaSysVehicle.setOwnerName(vehicleManageInfo.getOwnerName());
        oaSysVehicle.setCertigyDateA(vehicleManageInfo.getCertigyDateA());
        //oaSysVehicle.setFareType(vehicleManageInfo.getVehicleType());
        oaSysVehicle.setFuelType(vehicleManageInfo.getFuelType());
        oaSysVehicle.setEngineDisplace(vehicleManageInfo.getEngineDisplace());
        oaSysVehicle.setTransAgency(vehicleManageInfo.getTransAgency());
        oaSysVehicle.setTransArea(vehicleManageInfo.getTransArea());
        oaSysVehicle.setTransDateStart(vehicleManageInfo.getTransDateStart());
        oaSysVehicle.setTransDateStop(vehicleManageInfo.getTransDateStop());
        oaSysVehicle.setCertifyDateB(vehicleManageInfo.getCertifyDateB());
        oaSysVehicle.setFixStat(vehicleManageInfo.getFixStat());
        oaSysVehicle.setCheckStat(vehicleManageInfo.getCheckStat());
        oaSysVehicle.setFeePrintId(vehicleManageInfo.getFeePrintId());
        oaSysVehicle.setGpsBrand(vehicleManageInfo.getGpsBrand());
        oaSysVehicle.setGpsModel(vehicleManageInfo.getGpsModel());
        oaSysVehicle.setGpsInstallDate(vehicleManageInfo.getGpsInstallDate());
        oaSysVehicle.setRegisterDate(vehicleManageInfo.getRegisterDate());
        oaSysVehicle.setCommercialType("1");
        int i = oaSysVehicleMapper.updateById(oaSysVehicle);
        if (i > 0) {
            return Rv.wrap(EErrorCode.success);
        } else {
            return Rv.wrap(EErrorCode.beUsed);
        }
    }

    /**
     * 删除车辆信息
     *
     * @param vehicleId:车辆ID
     * @return
     */
    @Override
    public Rv deleteVehicle(Long vehicleId) {
        QueryWrapper<OaSysVehicle> queryWrapper = new QueryWrapper();
        queryWrapper.eq(OaSysVehicle.VEHICLE_ID, vehicleId);
        oaSysVehicleMapper.delete(queryWrapper);
        return Rv.wrap(EErrorCode.success);
    }

    /**
     * 分页模糊查询车辆信息
     *
     * @param current：要查询的页码
     * @return
     */
    @Override
    public Rv getVehicleListLike(Integer current, String veh_status, String companyId, String veh_type, String veh_no) {
        IPage iPage = new Page();
        iPage.setCurrent(current);
        iPage.setSize(20);
        //List<OaSysVehicleMdict> oaSysVehicleMdicts = this.findVehicleMdict();
        //List<VehicleManageInfo> vehicleListLike = oaSysVehicleMapper.getVehicleListLike(iPage, veh_status, companyId, veh_type, veh_no);
        //List<VehicleManageInfo> vehicleManageInfos = this.dataSplicing(vehicleListLike, oaSysVehicleMdicts);
        //iPage.setRecords(vehicleManageInfos);
        return Rv.wrap(EErrorCode.success, iPage);
    }

    /**
     * 查询数据导出Excel
     *
     * @return
     */
    @Override
    public Rv exportGetVehicle(VehicleManageInfo vehicleManageInfo) {
        // 查询车辆信息
        List<VehicleManageInfo> vehicleList = oaSysVehicleMapper.exportGetVehicle(
                vehicleManageInfo.getParamsVehicleNo(), vehicleManageInfo.getParamsCityCode(), vehicleManageInfo.getParamsVehFrameNumber(), vehicleManageInfo.getParamsCode(), vehicleManageInfo.getParamsDriveName()
        );
        if (vehicleList.isEmpty() || CollectionUtils.isEmpty(vehicleList)) {
            return Rv.wrap(EErrorCode.noData, EErrorCode.noData.getDesc());
        }
        List<VehicleManageInfo> list = getInfoList(vehicleList);
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(list));
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            if (CheckUtil.strIsNotEmpty(jsonObject.getString("brand"))) {
                jsonObject.put("vehicleName", jsonObject.getString("brand").concat("-" + jsonObject.getString("model")).concat("-" + jsonObject.getString("vehicleTypeName")));
            }
            if (CheckUtil.strIsNotEmpty(jsonObject.getString("province"))) {
                jsonObject.put("cityName", jsonObject.getString("province").concat("-" + jsonObject.getString("city")));
            }
            if (CheckUtil.objIsNotEmpty(jsonObject.getInteger("vehFuelType")) && jsonObject.getInteger("vehFuelType") < 5) {
                jsonObject.put("fuelType", EVehFuelType.findById(jsonObject.getInteger("vehFuelType")).getDesc());
            }
        }
        return Rv.wrap(EErrorCode.success, jsonArray);
    }

    /**
     * 品牌车系数据查询拼接
     *
     * @return
     */
    @Override
    public Rv brandCarSystem() {
        ////查询车辆关联表获取Code
        //QueryWrapper<OaVehicleSeries> queryWrapperOaVehicleSeries = new QueryWrapper<>();
        //List<OaVehicleSeries> queryWrapperOaVehicleSeriesList = oaVehicleSeriesMapper.selectList(queryWrapperOaVehicleSeries);
        //if (queryWrapperOaVehicleSeriesList.size() != 0) {
        //    List<Long> queryWrapperOaVehicleSeriesListLong = new ArrayList<>();
        //    for (int i = 0; i < queryWrapperOaVehicleSeriesList.size(); i++) {
        //        queryWrapperOaVehicleSeriesListLong.add(Long.valueOf(queryWrapperOaVehicleSeriesList.get(i).getCode()));
        //    }
        //    //根据车辆关联表Code获取主键mdict_id，查询第4层
        //    QueryWrapper<OaSysVehicleMdict> queryWrapperOaSysVehicleMdict4 = new QueryWrapper<>();
        //    queryWrapperOaSysVehicleMdict4.in(OaSysVehicleMdict.CODE, queryWrapperOaVehicleSeriesListLong);
        //    List<OaSysVehicleMdict> oaSysVehicleMdicts4 = oaSysVehicleMdictMapper.selectList(queryWrapperOaSysVehicleMdict4);
        //    List<Long> queryWrapperOaSysVehicleMdict4MdictId = new ArrayList<>();
        //    for (int i = 0; i < oaSysVehicleMdicts4.size(); i++) {
        //        queryWrapperOaSysVehicleMdict4MdictId.add(Long.valueOf(oaSysVehicleMdicts4.get(i).getParentId()));
        //    }
        //
        //    //根据车辆字典表mdict_i获取主键mdict_id，查询第3层
        //    List<Long> queryWrapperOaSysVehicleMdict3MdictId = new ArrayList<>();
        //    List<OaSysVehicleMdict> oaSysVehicleMdicts3 = getGradeVehicle(queryWrapperOaSysVehicleMdict4MdictId);
        //    for (int i = 0; i < oaSysVehicleMdicts3.size(); i++) {
        //        queryWrapperOaSysVehicleMdict3MdictId.add(Long.valueOf(oaSysVehicleMdicts3.get(i).getParentId()));
        //    }
        //
        //
        //    //根据车辆字典表mdict_i获取主键mdict_id，查询第2层
        //    List<Long> queryWrapperOaSysVehicleMdict2MdictId = new ArrayList<>();
        //    List<OaSysVehicleMdict> oaSysVehicleMdicts2 = getGradeVehicle(queryWrapperOaSysVehicleMdict3MdictId);
        //    for (int i = 0; i < oaSysVehicleMdicts2.size(); i++) {
        //        queryWrapperOaSysVehicleMdict2MdictId.add(Long.valueOf(oaSysVehicleMdicts2.get(i).getParentId()));
        //    }
        //
        //
        //    //根据车辆字典表mdict_i获取主键mdict_id，查询第2层
        //    List<Long> queryWrapperOaSysVehicleMdict1MdictId = new ArrayList<>();
        //    List<OaSysVehicleMdict> oaSysVehicleMdicts1 = getGradeVehicle(queryWrapperOaSysVehicleMdict2MdictId);
        //    for (int i = 0; i < oaSysVehicleMdicts1.size(); i++) {
        //        queryWrapperOaSysVehicleMdict1MdictId.add(Long.valueOf(oaSysVehicleMdicts1.get(i).getParentId()));
        //    }
        //
        //    List<Map<String, Object>> vehicleData = new ArrayList<>();
        //
        //    for (int i = 0; i < oaSysVehicleMdicts1.size(); i++) {
        //        OaSysVehicleMdict e = oaSysVehicleMdicts1.get(i);
        //        Map<String, Object> resMap = new HashMap<>();
        //        resMap.put("id", e.getMdictId());
        //        resMap.put("pId", e.getParentId());
        //        resMap.put("name", e.getName());
        //        resMap.put("code", e.getCode());
        //        vehicleData.add(resMap);
        //    }
        //
        //    for (int i = 0; i < oaSysVehicleMdicts2.size(); i++) {
        //        OaSysVehicleMdict e = oaSysVehicleMdicts2.get(i);
        //        Map<String, Object> resMap = new HashMap<>();
        //        resMap.put("id", e.getMdictId());
        //        resMap.put("pId", e.getParentId());
        //        resMap.put("name", e.getName());
        //        resMap.put("code", e.getCode());
        //        vehicleData.add(resMap);
        //    }
        //
        //    for (int i = 0; i < oaSysVehicleMdicts3.size(); i++) {
        //        OaSysVehicleMdict e = oaSysVehicleMdicts3.get(i);
        //        Map<String, Object> resMap = new HashMap<>();
        //        resMap.put("id", e.getMdictId());
        //        resMap.put("pId", e.getParentId());
        //        resMap.put("name", e.getName());
        //        resMap.put("code", e.getCode());
        //        vehicleData.add(resMap);
        //    }
        //
        //
        //    for (int i = 0; i < oaSysVehicleMdicts4.size(); i++) {
        //        OaSysVehicleMdict e = oaSysVehicleMdicts4.get(i);
        //        Map<String, Object> resMap = new HashMap<>();
        //        resMap.put("id", e.getMdictId());
        //        resMap.put("pId", e.getParentId());
        //        resMap.put("name", e.getName());
        //        resMap.put("code", e.getCode());
        //        vehicleData.add(resMap);
        //    }
        //    JSONArray menuResultList = ListToTreeUtil.listToTree(JSONArray.parseArray(JSON.toJSONString(vehicleData)), "id", "pId", "children");
        //    return Rv.wrap(EErrorCode.success, menuResultList);
        //} else {
        //    return Rv.wrap(EErrorCode.userFieldError);
        //}
        return null;
    }


    private List<OaSysVehicleMdict> getGradeVehicle(List<Long> mdictId) {
        QueryWrapper<OaSysVehicleMdict> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(OaSysVehicleMdict.MDICT_ID, mdictId);
        List<OaSysVehicleMdict> resultS = oaSysVehicleMdictMapper.selectList(queryWrapper);
        return resultS;
    }


    /**
     * 查询车字典
     *
     * @return
     */
    private List<OaSysVehicleMdict> findVehicleMdict(List<String> inList, String findField) {
        //查询车字典数据
        QueryWrapper<OaSysVehicleMdict> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(findField, inList);
        queryWrapper.select(OaSysVehicleMdict.MDICT_ID, OaSysVehicleMdict.PARENT_ID, OaSysVehicleMdict.CODE, OaSysVehicleMdict.NAME);
        return oaSysVehicleMdictMapper.selectList(queryWrapper);
    }

    /**
     * 查询城市字典
     *
     * @return
     */
    private List<OaSysArea> findCityMdict(List<String> inList, String findField) {
        //查询车字典数据
        QueryWrapper<OaSysArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(findField, inList);
        queryWrapper.select(OaSysArea.AREA_ID, OaSysArea.PARENT_ID, OaSysArea.NAME, OaSysArea.AD_CODE);
        return oaSysAreaMapper.selectList(queryWrapper);
    }


}
